<?

//Data loading
$title='JOURNAL TRANSACTION';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new GridPDFReport("P","mm","Letter");
$pdf->AddPage();
$Result = GridList();
if ($Result) $pdf->GridTable($Result);
$pdf->Output();


function GridList () {
        $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
        $WHERE  = BuildCondition (GetParam("source",""));

    if (GetParam("office","")) {
         $WHERE .= " AND ktrasl = '".GetParam("office","")."' ";
    }

    if (GetParam("fromDate","")) {
        list($FDay,$FMonth,$FYear) = explode("/",GetParam("fromDate",""));
          $WHERE .= " AND tglbkt>='$FYear-$FMonth-$FDay' ";
      }

    if (GetParam("toDate","")) {
         list($TDay,$TMonth,$TYear) = explode("/",GetParam("toDate",""));
         $WHERE .= " AND tglbkt<='$TYear-$TMonth-$TDay' ";
    }

    $SQL  = "SELECT * FROM ais_memorial WHERE 1 ".$WHERE;

      $SQL .= " ORDER BY nobkt ";
      //die($SQL);
    $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
      return $MyResult;
}

function BuildCondition ($no) {
    $row_number = explode(",",$no);
    //print $row_number[0];
    for ($x=0;$x<count($row_number);$x++) {
        if (strlen($WHERE)) $WHERE .= " OR nobkt like '$row_number[$x]%' ";
        else $WHERE = " nobkt like '$row_number[$x]%' ";
    }
    if (strlen($WHERE)) $WHERE = " AND (".$WHERE .") ";
    return $WHERE;
}

function BuildPeriod ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
  return $arraymonth[$month].", $year";
}


class GridPDFReport extends PDF {

    function Header() {
        global $title,$perush,$page,$DEFINE_FONT;
        if ($DEFINE_FONT==0) {
            $this->AddFont('arialn','','arialn.php');
            $DEFINE_FONT=1;
        }
        $this->SetFont('arialn','',10);
        $this->Cell(195,5,$perush,0,1,'C',0);
        $this->Cell(195,5,$title,0,1,'C',0);
        $this->Cell(195,5,"PERIOD: ".GetParam("fromDate","")." TO ".GetParam("toDate",""),0,1,'C',0);
        $this->Cell(195,5,$page." ".$this->PageNo(),0,1,'R',0);
        //Header
        //Column widths
        $w=array(25,25,50,20,20,10,10,20,20);
        $this->SetFont('arialn','',8);
        $this->Cell($w[0],7,"NO.BUKTI ",1,0,'C',0);
        $this->Cell($w[1],7,"NO.REF ",1,0,'C',0);
        $this->Cell($w[2],7,"DESCRIPTION",1,0,'C',0);
        $this->Cell($w[3],7,"SKADA",1,0,'C',0);
        $this->Cell($w[4],7,"DEBTUR",1,0,'C',0);
        $this->Cell($w[5],7,"UU",1,0,'C',0);
        $this->Cell($w[6],7,"CUR",1,0,'C',0);
        $this->Cell($w[7],7,"DEBET",1,0,'C',0);
        $this->Cell($w[8],7,"CREDIT",1,0,'C',0);
        $this->Ln();
        $this->SetFont('arialn','',8);
    }

    function GridTable($Result) {
        //Column widths
        $w=array(25,25,50,20,20,10,10,20,20);
        //Data
        foreach($Result as $row)
        {
            if ($row["dk"]=="D") $row["debet"] = $row["jumlah"];
            else $row["credit"] = $row["jumlah"];

            $totald = $totald + $row["debet"];
            $totalc = $totalc + $row["credit"];

            if (($LastNo<>$row["nobkt"]) && (strlen($LastNo)>0)) {
                $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4]+$w[5]+$w[6],7,"SUB TOTAL:",1,0,'R',0);
                $this->Cell($w[7],7,number_format($subtotald,2),1,0,'R',0);
                        $this->Cell($w[8],7,number_format($subtotalc,2),1,0,'R',0);
                $this->Ln();
                $subtotald = $row["debet"];
                $subtotalc = $row["credit"];
            } else {
                $subtotald = $subtotald + $row["debet"];
                $subtotalc = $subtotalc + $row["credit"];
            }


            if (($LastNo<>$row["nobkt"]) && (strlen($row["nobkt"])))
                $this->Cell($w[0],6,$row["nobkt"],'LR');
            else
                $this->Cell($w[0],6,"",'LR');

            $this->Cell($w[1],6,$row["noref"],'LR');
            $this->Cell($w[2],6,$row["uraian"],'LR');
            $this->Cell($w[3],6,$row["skada"],'LR');
            $this->Cell($w[4],6,$row["debtur"],'LR');
            $this->Cell($w[5],6,$row["kdact"],'LR');
            $this->Cell($w[6],6,$row["curr"],'LR');
            $this->Cell($w[7],6,number_format($row["debet"],2),'LR',0,'R');
            $this->Cell($w[8],6,number_format($row["credit"],2),'LR',0,'R');
            $this->Ln();


            $LastNo = $row["nobkt"];
        }
        $this->Cell(160,7,"SUB TOTAL:",1,0,'R',0);
        $this->Cell(20,7,number_format($subtotald,2),1,0,'R',0);
        $this->Cell(20,7,number_format($subtotalc,2),1,0,'R',0);
        $this->Ln();
        $this->Cell(160,7,"TOTAL:",1,0,'R',0);
        $this->Cell(20,7,number_format($totald,2),1,0,'R',0);
        $this->Cell(20,7,number_format($totalc,2),1,0,'R',0);
        $this->Ln();
        //Closure line
        //$this->Cell(array_sum($w),0,'','T');
    }

    function Footer() {
        //Closure line
        $this->Cell(200,0,'','T');
    }

}


?>
